<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Acme\DemoBundle\Entity\BaoCao;
use Symfony\Component\HttpFoundation\Response;

class BaoCaoController extends Controller
{
	public function thiduaAction() {
		
		$dateNow = new \DateTime('now');
		$yearNow = intval($dateNow->format("Y"));
		
		$listNhanVien =	$this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->getAll();
		$results = array();
		$object = array();
		$max= 0;
		foreach ( $listNhanVien as  $nhanvien) {
			$object[$nhanvien->getId()] = 0;
			for($i=$yearNow-1;$i>0;$i--){
				$thiDua = $this->getDoctrine()
				->getRepository('AcmeDemoBundle:Nhanvienthidua')
				->getByCondition($nhanvien->getId(),$i);
				if($thiDua) {
					if($thiDua->getThiduaid()->getId() == 8){
						$object[$nhanvien->getId()] = $object[$nhanvien->getId()] + 1; 
						$pos = $object[$nhanvien->getId()];
						if($pos > $max)
							$max = $pos;
						$results[$nhanvien->getId()] = array("ten"=>$nhanvien->getHoten(),
								"chucvu"=>$nhanvien->getChucvuid()->getTen(),
								"phongban"=>$nhanvien->getPhongbanid()->getTen(),
								"solan"=>$object[$nhanvien->getId()]);
					} else {
						break;
					}
				}else {
					break;
				}
			}
	
		}
		
		
		return $this->render(
				'AcmeDemoBundle:Demo:Baocaonamthidua.html.twig',
				array('results'=>$results,'max'=>$max)
		);
	}
	public function getbaocaovaodangAction()
	{

		$request = $this->get('request');


		$type = $request->request->get('type') ? strval($request->request->get('type')) : "";
		$from = $request->request->get('from') ? intval($request->request->get('from')) : -1;
		$to = $request->request->get('to') ? intval($request->request->get('to')) : -1;

		$page = $request->request->get('page') ? intval($request->request->get('page')) : 1;
		$rows = $request->request->get('rows') ? intval($request->request->get('rows')) : 10;


		$offset = ($page-1)*$rows;

		$listNhanVien =	$this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->baoCaoTuoiDang($offset,$rows,$type,$from,$to);

		$countNhanVien = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')->countBaoCaoTuoiDang($type,$from,$to);

		$result = array();
		$result["total"] = \Count( $countNhanVien);

		$items = array();
		$dateNow = new \DateTime('now');
		$yearNow = intval($dateNow->format("Y"));
		$monthNow = intval($dateNow->format("m"));
		$dayNow = intval($dateNow->format("d"));

		foreach ($listNhanVien as $nhanvien){
			$YearNV = 0;
			$YearNV = $yearNow - intval($nhanvien->getNgayvaodang()->format("Y")) ;
			if($monthNow<intval($nhanvien->getNgayvaodang()->format("m")))
				$YearNV = $YearNV - 1;
			if($monthNow==intval($nhanvien->getNgayvaodang()->format("m")) && $dayNow < intval($nhanvien->getNgayvaodang()->format("d")))
				$YearNV = $YearNV - 1;
			if($YearNV<1) $YearNV="<1";
			if($nhanvien->getTinhbaohiem() == "1"){
				$tinhbaohiem = "Có";
			} else {
				$tinhbaohiem = "Không";
			}

			if($nhanvien->getTinhantrua() == "1"){
				$tinhantrua = "Có";
			} else {
				$tinhantrua = "Không";
			}


			$ngaysinh = $nhanvien->getNgaysinh()->format('d-m-Y');
			$ngayvaolam = $nhanvien->getNgayvaolam()->format('d-m-Y');
			$ngayvaodang = $nhanvien->getNgayvaodang()->format('d-m-Y');

			$item = array("nam" => $YearNV,"hoten"=>$nhanvien->getHoten(),
					"diachi"=>$nhanvien->getDiachi(),
					"ngaysinh"=>$ngaysinh,
					"dienthoai"=>$nhanvien->getDienthoai(),
					"phongban"=>$nhanvien->getPhongbanid()->getTen(),
					"chucvu"=>$nhanvien->getChucvuid()->getTen(),
					"ngayvaolam"=>$ngayvaolam,
					"ngayvaodang"=>$ngayvaodang,
					"trinhdo"=>$nhanvien->getTrinhdoid()->getTen(),
					"mucluong"=>$nhanvien->getMucluong(),
					"hesoluongcv"=>$nhanvien->getHesoluongcv(),
					"tinhbaohiem"=>$tinhbaohiem,
					"tinhantrua"=>$tinhantrua,
					"tienphucap"=>$nhanvien->getTienphucap()
			);

			array_push($items, $item);
		}

		$result["rows"] = $items;


		$response = new Response(json_encode($result));
		$response->headers->set('Content-Type', 'application/json');

		return $response;

	}
	public function getbaocaocongtacAction()
	{

		$request = $this->get('request');


		$type = $request->request->get('type') ? strval($request->request->get('type')) : "";
		$from = $request->request->get('from') ? intval($request->request->get('from')) : -1;
		$to = $request->request->get('to') ? intval($request->request->get('to')) : -1;

		$page = $request->request->get('page') ? intval($request->request->get('page')) : 1;
		$rows = $request->request->get('rows') ? intval($request->request->get('rows')) : 10;


		$offset = ($page-1)*$rows;

		$listNhanVien =	$this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->baoCaoNamCongTac($offset,$rows,$type,$from,$to);

		$countNhanVien = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')->countBaoCaoNamCongTac($type,$from,$to);

		$result = array();
		$result["total"] = \Count( $countNhanVien);

		$items = array();
		$dateNow = new \DateTime('now');
		$yearNow = intval($dateNow->format("Y"));
		$monthNow = intval($dateNow->format("m"));
		$dayNow = intval($dateNow->format("d"));

		foreach ($listNhanVien as $nhanvien){
			$YearNV = 0;
			$YearNV = $yearNow - intval($nhanvien->getNgayvaolam()->format("Y")) ;
			if($monthNow<intval($nhanvien->getNgayvaolam()->format("m")))
				$YearNV = $YearNV - 1;
			if($monthNow==intval($nhanvien->getNgayvaolam()->format("m")) && $dayNow < intval($nhanvien->getNgayvaolam()->format("d")))
				$YearNV = $YearNV - 1;
			if($YearNV<1) $YearNV="<1";
			if($nhanvien->getTinhbaohiem() == "1"){
				$tinhbaohiem = "Có";
			} else {
				$tinhbaohiem = "Không";
			}

			if($nhanvien->getTinhantrua() == "1"){
				$tinhantrua = "Có";
			} else {
				$tinhantrua = "Không";
			}


			$ngaysinh = $nhanvien->getNgaysinh()->format('d-m-Y');
			$ngayvaolam = $nhanvien->getNgayvaolam()->format('d-m-Y');
			if($nhanvien->getNgayvaodang()){
				$ngayvaodang = $nhanvien->getNgayvaodang()->format('d-m-Y');
			} else {
				$ngayvaodang = "";
			}
				

			$item = array("nam" => $YearNV,"hoten"=>$nhanvien->getHoten(),
					"diachi"=>$nhanvien->getDiachi(),
					"ngaysinh"=>$ngaysinh,
					"dienthoai"=>$nhanvien->getDienthoai(),
					"phongban"=>$nhanvien->getPhongbanid()->getTen(),
					"chucvu"=>$nhanvien->getChucvuid()->getTen(),
					"ngayvaolam"=>$ngayvaolam,
					"ngayvaodang"=>$ngayvaodang,
					"trinhdo"=>$nhanvien->getTrinhdoid()->getTen(),
					"mucluong"=>$nhanvien->getMucluong(),
					"hesoluongcv"=>$nhanvien->getHesoluongcv(),
					"tinhbaohiem"=>$tinhbaohiem,
					"tinhantrua"=>$tinhantrua,
					"tienphucap"=>$nhanvien->getTienphucap()
			);

			array_push($items, $item);
		}

		$result["rows"] = $items;


		$response = new Response(json_encode($result));
		$response->headers->set('Content-Type', 'application/json');

		return $response;

	}

	public function getbaocaonhanlucAction()
	{

		$request = $this->get('request');
		if($request->request->get('type')){


			$type = $request->request->get('type') ? strval($request->request->get('type')) : "";

			$page = $request->request->get('page') ? intval($request->request->get('page')) : 1;
			$rows = $request->request->get('rows') ? intval($request->request->get('rows')) : 10;


			$offset = ($page-1)*$rows;


			$listNhanVien =	$this->getDoctrine()
			->getRepository('AcmeDemoBundle:NhanVien')
			->findForBaoCaoNhanLuc($offset,$rows,$type);

			$countNhanVien = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:NhanVien')->countAllForBaoCaoNhanLuc($type);

			$result = array();
			$result["total"] = \Count( $countNhanVien);

			$items = array();


			foreach ($listNhanVien as $nhanvien){

				if($nhanvien->getTinhbaohiem() == "1"){
					$tinhbaohiem = "Có";
				} else {
					$tinhbaohiem = "Không";
				}

				if($nhanvien->getTinhantrua() == "1"){
					$tinhantrua = "Có";
				} else {
					$tinhantrua = "Không";
				}


				$ngaysinh = $nhanvien->getNgaysinh()->format('d-m-Y');
				$ngayvaolam = $nhanvien->getNgayvaolam()->format('d-m-Y');
				if($nhanvien->getNgayvaodang()){
					$ngayvaodang = $nhanvien->getNgayvaodang()->format('d-m-Y');
				} else {
					$ngayvaodang = "";
				}

				$item = array("hoten"=>$nhanvien->getHoten(),
						"diachi"=>$nhanvien->getDiachi(),
						"ngaysinh"=>$ngaysinh,
						"dienthoai"=>$nhanvien->getDienthoai(),
						"phongban"=>$nhanvien->getPhongbanid()->getTen(),
						"chucvu"=>$nhanvien->getChucvuid()->getTen(),
						"ngayvaolam"=>$ngayvaolam,
						"ngayvaodang"=>$ngayvaodang,
						"trinhdo"=>$nhanvien->getTrinhdoid()->getTen(),
						"mucluong"=>$nhanvien->getMucluong(),
						"hesoluongcv"=>$nhanvien->getHesoluongcv(),
						"tinhbaohiem"=>$tinhbaohiem,
						"tinhantrua"=>$tinhantrua,
						"tienphucap"=>$nhanvien->getTienphucap()
				);

				array_push($items, $item);
			}

			$result["rows"] = $items;


			$response = new Response(json_encode($result));
			$response->headers->set('Content-Type', 'application/json');

			return $response;
		} else {
			$result = array();
			$result["total"] = 0;
			$result["rows"] = "";
			$response = new Response(json_encode($result));
			$response->headers->set('Content-Type', 'application/json');

			return $response;
		}

	}

	public function getbaocaotrinhdoAction()
	{

		$request = $this->get('request');
		if($request->request->get('name')){


			$name = $request->request->get('name') ? strval($request->request->get('name')) : "";

			$page = $request->request->get('page') ? intval($request->request->get('page')) : 1;
			$rows = $request->request->get('rows') ? intval($request->request->get('rows')) : 10;


			$offset = ($page-1)*$rows;
				
			$trinhdo = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:TrinhDo')
			->findOneByTen($name);

			$listNhanVien =	$this->getDoctrine()
			->getRepository('AcmeDemoBundle:NhanVien')
			->findForBaoCaoTrinhDo($offset,$rows,$trinhdo->getId());

			$countNhanVien = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:NhanVien')->countAllForBaoCaoTrinhDo($trinhdo->getId());

			$result = array();
			$result["total"] = \Count( $countNhanVien);

			$items = array();


			foreach ($listNhanVien as $nhanvien){

				if($nhanvien->getTinhbaohiem() == "1"){
					$tinhbaohiem = "Có";
				} else {
					$tinhbaohiem = "Không";
				}

				if($nhanvien->getTinhantrua() == "1"){
					$tinhantrua = "Có";
				} else {
					$tinhantrua = "Không";
				}


				$ngaysinh = $nhanvien->getNgaysinh()->format('d-m-Y');
				$ngayvaolam = $nhanvien->getNgayvaolam()->format('d-m-Y');
				if($nhanvien->getNgayvaodang()){
					$ngayvaodang = $nhanvien->getNgayvaodang()->format('d-m-Y');
				} else {
					$ngayvaodang = "";
				}

				$item = array("hoten"=>$nhanvien->getHoten(),
						"diachi"=>$nhanvien->getDiachi(),
						"ngaysinh"=>$ngaysinh,
						"dienthoai"=>$nhanvien->getDienthoai(),
						"phongban"=>$nhanvien->getPhongbanid()->getTen(),
						"chucvu"=>$nhanvien->getChucvuid()->getTen(),
						"ngayvaolam"=>$ngayvaolam,
						"ngayvaodang"=>$ngayvaodang,
						"trinhdo"=>$nhanvien->getTrinhdoid()->getTen(),
						"mucluong"=>$nhanvien->getMucluong(),
						"hesoluongcv"=>$nhanvien->getHesoluongcv(),
						"tinhbaohiem"=>$tinhbaohiem,
						"tinhantrua"=>$tinhantrua,
						"tienphucap"=>$nhanvien->getTienphucap()
				);

				array_push($items, $item);
			}

			$result["rows"] = $items;


			$response = new Response(json_encode($result));
			$response->headers->set('Content-Type', 'application/json');

			return $response;
		} else {
			$result = array();
			$result["total"] = 0;
			$result["rows"] = "";
			$response = new Response(json_encode($result));
			$response->headers->set('Content-Type', 'application/json');

			return $response;
		}

	}

	public function nhanlucAction() {

		$from = "";
		$to= "";
		$request = $this->get('request');
		if ($request->isMethod('POST')) {

			$fromRequest = $request->request->get('tungay');
			if($fromRequest != "") {
				$arr = explode('-', $fromRequest);
				$from = $arr[2].'-'.$arr[1].'-'.$arr[0];
			}

			$toRequest  = $request->request->get('denngay');
			if($toRequest != "") {
				$arr = explode('-', $toRequest);
				$to = $arr[2].'-'.$arr[1].'-'.$arr[0];
			}

		}


		$listNhanVien =	$this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->findByDate($from,$to);
		$tongnv = \Count($listNhanVien);
		//20-30
		$tuoidoi1 = 0;
		//30-40
		$tuoidoi2 = 0;
		//40-50
		$tuoidoi3 = 0;
		//50-60
		$tuoidoi4 = 0;
		$phantram1= 0;
		$phantram2= 0;
		$phantram3= 0;
		$phantram4= 0;
		if($tongnv>0){
			$dateNow = new \DateTime('now');
			$yearNow = intval($dateNow->format("Y"));


			foreach ( $listNhanVien as  $nhanvien) {

				$yearNV = intval($nhanvien->getNgaysinh()->format("Y"));
				if($yearNow - $yearNV <= 30){
					$tuoidoi1++;
				} else if($yearNow - $yearNV <= 40){
					$tuoidoi2++;
				} else if($yearNow - $yearNV <= 50){
					$tuoidoi3++;
				} else {
					$tuoidoi4++;
				}

			}
			$phantram1 = Round($tuoidoi1*100/$tongnv,2);

			$phantram2 = Round($tuoidoi2*100/$tongnv,2);

			$phantram3 = Round($tuoidoi3*100/$tongnv,2);

			$phantram4 = 100 - ($phantram1 + $phantram2 + $phantram3);
		}

		$arrayTuoi = array('tongnv'=>$tongnv,
				'tuoidoi1'=>$tuoidoi1,'tuoidoi2'=>$tuoidoi2,'tuoidoi3'=>$tuoidoi3,'tuoidoi4'=>$tuoidoi4,
				'phantram1'=>$phantram1,'phantram2'=>$phantram2,'phantram3'=>$phantram3,'phantram4'=>$phantram4
		);

		return $this->render(
				'AcmeDemoBundle:Demo:Baocaonhanluc.html.twig',
				array('from'=>$from,'to'=>$to,'arrTuoi' => $arrayTuoi)
		);
	}

	public function congtacAction() {

		$lstcongtac = array();

		$dateNow = new \DateTime('now');
		$yearNow = intval($dateNow->format("Y"));
		$monthNow = intval($dateNow->format("m"));
		$dayNow = intval($dateNow->format("d"));

		$max =	$this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->getMaxYear();

		$min =	$this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->getMinYear();


		$maxYear = 0;
		$maxName = "";

		
			$maxName = $max->getHoten();
			$maxYear = $yearNow - intval($max->getNgayvaolam()->format("Y")) ;
			if($monthNow<intval($max->getNgayvaolam()->format("m")))
				$maxYear--;
				
			if($monthNow==intval($max->getNgayvaolam()->format("m")) && $dayNow < intval($max->getNgayvaolam()->format("d")))
				$maxYear--;
				
		

		$minYear = 0;
		$minName = "";
		
			$minName = $min->getHoten();
			$minYear = ($yearNow - intval($min->getNgayvaolam()->format("Y"))) < 1 ? "<1" : $yearNow - intval($min->getNgayvaolam()->format("Y")) ;
				
		


		for ($i=$maxYear;$i>=0;$i--){
			$sl = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:NhanVien')
			->countBaoCaoNamCongTac("1",$i,"");
			if($i == 0) {
				array_push($lstcongtac,array("nam"=>"<1","sl"=>\Count($sl)));
			} else {
				array_push($lstcongtac,array("nam"=>$i,"sl"=>\Count($sl)));
			}
		}


		return $this->render(
				'AcmeDemoBundle:Demo:Baocaonamcongtac.html.twig',
				array('maxName'=>$maxName,'maxYear'=>$maxYear,'minName'=>$minName,'minYear'=>$minYear,'lstcongtac' => $lstcongtac)
		);
	}
	public function tuoidangAction() {

		$lsttuoidang = array();

		$dateNow = new \DateTime('now');
		$yearNow = intval($dateNow->format("Y"));
		$monthNow = intval($dateNow->format("m"));
		$dayNow = intval($dateNow->format("d"));

		$max =	$this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->getTuoiDangMaxYear();

		$min =	$this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->getTuoiDangMinYear();


		$maxYear = 0;
		$maxName = "";

		$maxName = $max->getHoten();
		$maxYear = $yearNow - intval($max->getNgayvaodang()->format("Y"));
		if($monthNow<intval($max->getNgayvaodang()->format("m")))
			$maxYear--;

		if($monthNow==intval($max->getNgayvaodang()->format("m")) && $dayNow < intval($max->getNgayvaodang()->format("d")))
			$maxYear--;
		;


		$minYear = 0;
		$minName = "";

		$minName = $min->getHoten();
		$minYear = ($yearNow - intval($min->getNgayvaodang()->format("Y"))) < 1 ? "<1" : $yearNow - intval($min->getNgayvaodang()->format("Y")) ;




		for ($i=$maxYear;$i>=0;$i--){
			$sl = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:NhanVien')
			->countBaoCaoTuoiDang("1",$i,"");
			if($i == 0) {
				array_push($lsttuoidang,array("nam"=>"<1","sl"=>\Count($sl)));
			} else {
				array_push($lsttuoidang,array("nam"=>$i,"sl"=>\Count($sl)));
			}
		}


		return $this->render(
				'AcmeDemoBundle:Demo:Baocaonamvaodang.html.twig',
				array('maxName'=>$maxName,'maxYear'=>$maxYear,'minName'=>$minName,'minYear'=>$minYear,'lsttuoidang' => $lsttuoidang)
		);
	}

	public function trinhdoAction() {

		$listNhanVien =	$this->getDoctrine()
		->getRepository('AcmeDemoBundle:NhanVien')
		->countAllForBaoCaoTrinhDo(0);
		$tongnv = \Count($listNhanVien);

		$lstTrinhDo = array();

		$lstMaster = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:TrinhDo')->findAllByCoditions();

		$lstCount = array();
		foreach ( $lstMaster as  $master) {
			$lstCount[$master->getId()] = 0;
		}


		foreach ( $listNhanVien as  $nhanvien) {

			$lstCount[$nhanvien->getTrinhdoid()->getId()] = $lstCount[$nhanvien->getTrinhdoid()->getId()] + 1;

		}

		$count = 0;
		$phantram = 0;
		foreach ( $lstMaster as  $master) {
			$count++;
			if(Count($lstMaster)>0){

					
				if($count >= \Count($lstMaster)){
					array_push($lstTrinhDo,array("ten"=>$master->getTen(),"phantram"=> 100 - $phantram));
				} else {
					$phantram += Round($lstCount[$master->getId()]*100/$tongnv,2);
					array_push($lstTrinhDo,array("ten"=>$master->getTen(),"phantram"=> Round($lstCount[$master->getId()]*100/$tongnv,2)));
				}
			}
		}


		return $this->render(
				'AcmeDemoBundle:Demo:Baocaotrinhdo.html.twig',
				array('lstTrinhDo' => $lstTrinhDo,'tongnv'=>$tongnv)
		);
	}
}
